﻿//----------------------------------------------------------------------------------------------------
// A NodeInstance is the representation of a node in a level
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	import HG.Debug;
	import HG.GameService;
	import HG.Managers.LevelManager;
	import HG.Managers.PlayerManager;
	
	public class VisualConnection extends VisualObject
	{
		var m_ID:Number;
		
		var m_isEstablished:Boolean;
		var m_establishTarget:Number;
		var m_establishAnimTimer:Timer;
		
		var m_source:VisualAttachPoint;
		var m_target:VisualAttachPoint;
		
		var m_levelManager:LevelManager;
		var m_playerManager:PlayerManager;
		
		//Status checks
		var m_toEnemy:Boolean;
		var m_toFriend:Boolean;
		var m_fromEnemy:Boolean;
		var m_fromFriend:Boolean;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualConnection(ID:Number)
		{
			super();
			
			m_ID = ID;
			
			Line.Content.visible = false;
			Icon.visible = true;
			
			Line.Content.Green.visible = false;
			Line.Content.Red.visible = false;
			
			m_toEnemy = false;
			m_toFriend = false;
			m_fromEnemy = false;
			m_fromFriend = false;
			
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			
			Line.Content.DirBtn.visible = m_levelManager.GetEditorMode();
			Line.Content.Dir.visible = m_levelManager.GetEditorMode();
			Line.Content.Area.visible = m_levelManager.GetEditorMode();
			
			Line.Content.Dir.visible = false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetID():Number
		{
			return m_ID;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetType():String
		{
			return "Connection";
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ClearSource()
		{
			m_source = undefined;
			m_target = undefined;
			Draw();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Update()
		{
			// Get visual nodes on both sides.
			
			var fromID:Number = m_source.GetVisualNodeID();
			var fromNode:VisualNode = m_levelManager.GetVisualNode(fromID);
			
			var toID:Number = m_target.GetVisualNodeID();
			var toNode:VisualNode = m_levelManager.GetVisualNode(toID);
			
			var m_playerID:Number = m_playerManager.GetCurrentPlayerID();
			
			
			// direction --------------------------------------------------------
			var from_to:Boolean = fromNode.GetIsConnectedTo(toID);
			var to_from:Boolean = toNode.GetIsConnectedTo(fromID);
			
			if (from_to && !to_from)
			{
				Line.Content.Dir.visible = true;
				Line.Content.Dir.scaleX = -1;
			}
			else if (to_from && !from_to)
			{
				Line.Content.Dir.visible = true;
				Line.Content.Dir.scaleX = 1;
			}
			else
			{
				Line.Content.Dir.visible = false;
			}
			
			
			
			// Friend or foe --------------------------------------------------------
			var from_hasFriend:Boolean = fromNode.GetIsOcupiedBy(m_playerID);
			var from_hasEnemy:Boolean = fromNode.GetIsOcupiedByAnyoneBut(m_playerID);
			var to_hasFriend:Boolean = toNode.GetIsOcupiedBy(m_playerID);
			var to_hasEnemy:Boolean = toNode.GetIsOcupiedByAnyoneBut(m_playerID);
			
			
			if (from_hasFriend != m_fromFriend
			 || to_hasFriend != m_toFriend)
			{
				// Something has changed.
				if (from_hasFriend && to_hasFriend)
				{
					DoConnectAnim(true, m_fromFriend);
				}
				else if (!from_hasFriend && !to_hasFriend)
				{
					Line.Content.Green.visible = false;;
				}
			
			}
			
			
			if (from_hasEnemy != m_fromEnemy
			|| to_hasEnemy != m_toEnemy)
			{
				if (from_hasEnemy && to_hasEnemy)
				{
					DoConnectAnim(false, m_fromEnemy);
				}
				else if (!from_hasEnemy && !to_hasEnemy)
				{
					Line.Content.Red.visible = false;
				}
			}
				
				
			m_toEnemy = to_hasEnemy;
			m_toFriend = to_hasFriend;
			m_fromEnemy = from_hasEnemy;
			m_fromFriend = from_hasFriend;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetSource(attach:VisualAttachPoint, temporary:Boolean)
		{
			m_source = attach;
			
			Draw();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetSource():VisualAttachPoint
		{
			return m_source;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetTarget(attach:VisualAttachPoint, temporary:Boolean)
		{
			m_target = attach;
			
			Draw();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetTarget():VisualAttachPoint
		{
			return m_target;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Connects(from:Number, to:Number):Boolean
		{
			if (m_source.GetID() == from && m_target.GetID() == to)
			{
				return true;
			}
			
			if (m_source.GetID() == to && m_target.GetID() == from)
			{
				return true;
			}
			
			return false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ConnectsTo(to:Number):Boolean
		{
			var fromID:Number = m_source.GetVisualNodeID();
			var fromNode:VisualNode = m_levelManager.GetVisualNode(fromID);
			
			var toID:Number = m_target.GetVisualNodeID();
			var toNode:VisualNode = m_levelManager.GetVisualNode(toID);
			
			var to_from:Boolean = toNode.GetIsConnectedTo(fromID);
			var from_to:Boolean = fromNode.GetIsConnectedTo(toID);
			
			if (m_source.GetID() == to && from_to)
			{
				return true
			}
			else if (m_target.GetID() == to && to_from)
			{
				return true;
			}
			
			return false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function DoConnectAnim(friendly:Boolean, dirForward:Boolean)
		{
			var frame = "in_f";
			if (!dirForward)
				frame = "in_b"
			
			if (friendly)
			{
				Line.Content.Green.visible = true;
				Line.Content.Green.gotoAndPlay(frame);
			}
			else
			{
				Line.Content.Red.visible = true;
				Line.Content.Red.gotoAndPlay(frame);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function Draw()
		{
			if (m_source && m_target)
			{
				this.x = m_source.x;
				this.y = m_source.y;
				
				Line.Content.visible = true;
				Icon.visible = false;
				
				var dx:Number = m_target.x - m_source.x;
				var dy:Number = m_target.y - m_source.y;
				
				Line.Content.rotation = Math.atan2(dy, dx)*(180 / Math.PI);
			}
			else
			{
				Line.Content.visible = false;
				Icon.visible = true;
			}
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}





